package cn.itsource.ymcc.producer;


import cn.itsource.ymcc.constant.MediaConstants;
import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.result.JsonResult;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class MediaProducerImpl implements MediaProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public JsonResult handleFile2m3u8(MediaFile mediaFile) {
        //发送消息
        rocketMQTemplate.asyncSend(MediaConstants.TOPIC_COURSE_VIDEO_PUSH_TO_SRS + ":" + MediaConstants.TAGS_COURSE_VIDEO_PUSH_TO_SRS,
                MessageBuilder.withPayload(JSON.toJSONString(mediaFile)).build(), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println("发送成功");
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        System.out.println("发送失败");
                    }
                });
        return JsonResult.success();
    }
}
